<?
class monthwidget extends select {
	public $months = array(1,2,3,4,5,6,7,8,9,10,11,12);
	public $monthnames = array ('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september','october', 'november', 'december');
	public $daysinmonth = array (
		1 => 31,
		2 => 28,
		3 => 31,
		4 => 30,
		5 => 31,
		6 => 30,
		7 => 31,
		8 => 31,
		9 => 30,
		10 => 31,
		11 => 30,
		12 => 31
	);
	
	public $numdays;	
	public $displayFormat = 'Mon'; // Mon|mon|month|Month|Numeric
	public $month;
	
	public function __construct($month = ''){
		parent::__construct();
		if ( strlen($month)){
			if ( is_numeric($month)){
				if ( in_array($month, $this->months)){
					$this->month = $month;
				} else{
					trigger_error("The month {$month} supplied is invalid 1");
				}
			} elseif ( is_string($month)){
				$month = trim(strtolower($month));
				if ( in_array($month, $this->monthnames)){
					$this->month = array_search($month, $this->monthnames);
				} else {
					$found = 0;
					if ( strlen($month) == 3 ){
						foreach ( $this->monthnamess as $monthminus1 => $monthname ){
							$shortname = substr($monthname, 0, 3);
							if ( $shortname == $month){
								$this->month = $monthminus1 + 1;
								$found = 1;
								break;
							}
						}
					}
					if ( $found) {
						//ok
					} else {
						trigger_error("The month {$month} supplied is invalid 2");
					}
				}
			} else {
				trigger_error("The month {$month} supplied is invalid 3");
			}
		}
	}
	
	public function __toString(){
		foreach ( $this->monthnames as $num => $monthname){
			$fixednum = $num + 1;		
			if( $this->displayFormat == 'MON' ){
				$this->values[$fixednum] = substr(strtoupper($monthname), 0, 3);
			} elseif ( $this->displayFormat == 'Mon' ){
				$this->values[$fixednum] = substr(ucfirst($monthname), 0, 3);
			} elseif ( $this->displayFormat == 'mon' ){
				$this->values[$fixednum] = substr($monthname, 0, 3);				
			} elseif ( $this->displayFormat == 'Month' ){
				$this->values[$fixednum] = ucfirst($monthname);
			} elseif ( $this->displayFormat == 'month' ){
				$this->values[$fixednum] = $monthname;				
			} elseif (  $this->displayFormat == 'numeric' ){
				$this->values[$fixednum] = $fixednum;
			} else {
				$this->values[$fixednum] =substr($monthname, 0, 3);
			}
		}
		if ( in_array($this->month, $this->months)){
			$this->setSelected($this->month);
		}
		return parent::__toString();
	}
	
	public function setFormat($string){
		$validformats = array('Mon','mon', 'month', 'Month', 'Numeric', 'numeric', 'num');
		if ( in_array($string, $validformats)){
			$this->displayFormat = $string;
		}
	}

}
?>